package com.hps.ssh.controller;

import com.hps.ssh.entity.User;
import com.hps.ssh.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/list")
    public String list() {
        List<User> users = userService.getAll();
        System.out.println(users);
        return "list";
    }

    @RequestMapping("/add")
    public String add() {
        User user = new User();
        user.setUsername("张三");
        user.setPassword("123456");
        userService.add(user);
        return "add";
    }

    @RequestMapping("/{id}/get")
    @ResponseBody
    public User get(@PathVariable("id") Integer id) {
        return userService.getById(id);
    }

    @RequestMapping("/{id}/delete")
    public String delete(@PathVariable("id") Integer id, Model model) {
        userService.deleteById(id);
        model.addAttribute("tip", "delete success");
        return "success";
    }
}
